# QuestionDialog - This screen displays a question label and buttons YES and NO.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import clutter

from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.justified_text_menu_item import JustifiedTextMenuItem

class QuestionDialog(Screen):
    """
    QuestionDialog
    
    Dialog screen that contains question label and two buttons: yes and no.
    This is used when we want confirmation from the user for some operation.
    """
    
    def __init__(self, ui, frontend, data):
        """
        Initialize screen
        
        @param frontend: FrontendClient object
        @param theme: Theme object
        @param data: Userdata [0]=screen, [1]=callback, [2]=question string
        """
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.current_screen = data[0]
        self.callback = data[1]
        self.group = clutter.Group()
        
        #FIXME: Replace rect with beautiful cairo graphics
        rect = clutter.Rectangle()
        rect.set_color((0,0,0,233))
        rect.set_size(740,200)
        rect.set_position(280,280)
        rect.show()
        self.group.add(rect)
        
        question = clutter.Label()
        question.set_text(data[2])
        question.set_font_name(self.theme.getFont() + ' 32px')
        question.set_color(self.theme.getColor("title"))
        question.set_position(300,300)
        question.set_width(700)
        question.show()
        self.group.add(question)
        
        self.buttons = TextMenu(self.theme, self.frontend.get_configuration().show_effects())
        self.buttons.set_orientation(self.buttons.HORIZONTAL)
        self.buttons.set_item_size(350, 60)
        self.buttons.set_visible_column_count(2)
        
        yes = JustifiedTextMenuItem(350, 60, self.theme, "Yes", JustifiedTextMenuItem.CENTER)
        yes.set_userdata("yes")
        self.buttons.add_actor(yes)
        
        no = JustifiedTextMenuItem(350, 60, self.theme, "No", JustifiedTextMenuItem.CENTER)
        no.set_userdata("no")
        self.buttons.add_actor(no)
        
        self.buttons.set_position(300, 380)
        self.buttons.set_active(True)
        self.buttons.show()
        self.group.add(self.buttons)
        
        self.group.show()
        ui.getStage().add(self.group)
    
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.DIALOG
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "QuestionDialog"
    
    def handle_key_press_event(self, event):   
        """ Handle key pressed event. This can be transmitted to the widgets."""
        #LEFT KEY
        if event.keyval == clutter.keysyms.Left:
            self.buttons.move(self.buttons.LEFT)
        
        # RIGHT KEY
        elif event.keyval == clutter.keysyms.Right:
            self.buttons.move(self.buttons.RIGHT)
            
        # ENTER KEY
        elif event.keyval == clutter.keysyms.Return:
            if  self.buttons.get_current_menuitem().get_userdata() == "yes":
                self.callback()
                self.ui.changeScreen(self.current_screen, Transition.BACKWARD) # This is not always possible
            else:
                self.ui.changeScreen(self.current_screen, Transition.BACKWARD)